﻿package src.computation.distributionLaw 
{
	
	/**
	* 
	* Reliability project
	* -----------------------
	* @author  Dmitrij Boycov
	* @mail    dmitry.boycov@gmail.com
	* @date    April 27, 2010
	* @version 1
	* 
	*/
	
	import 	fl.controls.Label;	
	// src
	import src.computation.distributionLaw.AbstractLaw;
	import src.util.Utils;
	// astra
	import com.yahoo.astra.containers.formClasses.FormItem;	
	
	public class ExpLaw extends AbstractLaw{
		
		private var intensity;
		private var intensityTf;	
		public static var TYPE = "exp";
		
		public function ExpLaw() {
			super();		
			//
			this.intensityTf = Utils.numberInputText();
			if (this.intensity != null) {
				intensityTf.text = this.intensity;
			}
			
			var probabilityItem = new FormItem("Интенсивность", intensityTf);
			probabilityItem.labelWidth = 90;
			this.addChild(probabilityItem);				
			this.desc = "Exp. Probability depend on time by exp. law.";
		}
		
		public override function getProbabilty(time =null) {
			var t = this.time;
			trace("time" + time);
			if (time != null) {				
				t = time as Number;
			}
			this.intensity = Number(this.intensityTf.text);			
			trace("intens:" + this.intensity);
			trace("time" + t);
			trace("pr:"+Math.exp( -1 * this.intensity * t));
			return Math.exp( -1 * this.intensity * t);
		}
		
		public override function random():Number {
			if(this.intensity == null){
				this.intensity = Number(this.intensityTf.text);
			}
			var p = Math.random();
			return (-1) / this.intensity * Math.log(p);
		}		
		
		public override function toXML():String {
			this.intensity = this.intensityTf.text as Number;
			var str = "<law>";
			str += "<type>" + ExpLaw.TYPE + "</type>";
			str += "<params><intensity>" + this.intensity + "</intensity></params>";
			str += "</law>";
			return str;
		}
	
	}
	
}